//
//  CHHomeViewController.m
//  SZTZ
//
//  Created by 陈浩 on 2017/3/6.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHHomeViewController.h"
#import "CHAlertView.h"
#import "UITabBar+badge.h"

@interface CHHomeViewController ()<CHPopViewDelegate,UIAlertViewDelegate>
{
    NSString *cdnpath;
}
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,strong) CHItemButton *itemBtn;
@property (nonatomic ,assign) BOOL isReload;
@property (nonatomic ,strong) UIAlertView *myAlertView;
//@property (nonatomic ,strong) CHAlertView *myAlertView;
@end

@implementation CHHomeViewController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _isReload = NO;
    self.itemBtn = [CHItemButton buttonWithType:UIButtonTypeCustom];
    self.itemBtn.frame = CGRectMake(0, 0, 100, 40);
    self.itemBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.itemBtn setImage:[UIImage imageNamed:@"pull"] forState:UIControlStateNormal];
    [self.itemBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.itemBtn];;
//    
//    NSString *leapName = LeapAPPName;
//    [self checkVersion:leapName];
    

    
   
}




-(void)loadData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"type"] = @"fgwoa_sjtj_gdzctz";
    [[Globle getInstance].service RequestWithServiceName:@"fgwoa_getVersionByState" params:params httpMethod:@"GET" resultIsDictionary:true completeBlock:^(id result) {
        
        NSLog(@"%@",result);
        if (result) {
            [self.datas removeAllObjects];
            for (NSMutableDictionary *dict in result) {
                CHDateModel *model = [CHDateModel objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            CHDateModel *model = self.datas[0];
            NSString *title = [NSString stringWithFormat:@"%@年%@月",model.nf,model.yf];
            [self.itemBtn setTitle:title forState:UIControlStateNormal];
            [self refreshDataWithModel:model];
        }else{
            
            [self.webView removeFromSuperview];
            [self showNotice];
        }
        _isReload = YES;
        
    }];
}


-(void)rightBtnClick:(CHItemButton *)btn{
    
    CGFloat popViewH = 200;
    
    if (self.datas.count * 44 < 200) {
        popViewH = self.datas.count * 44;
    }
    
    CHPopView *popView = [[CHPopView alloc] initWithFrame:CGRectMake(UIScreenW - 150 -10, 74, 150, popViewH)];
    popView.delegate = self;
    popView.layer.cornerRadius = 5;
    popView.layer.masksToBounds = YES;
    popView.isSection = NO;
    popView.datas = self.datas;
    [popView presentPopViewToView:btn];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (!_isReload) {
        [self loadData];
    }
    
    
    
    //    [self isRefreshData];
    
}

//-(void)isRefreshData{
//
//
//    NSString *url = @"http://203.91.46.83:8030/SZFGWAPP/restservices/Rsss/fgwoa_checkVersion/query?username=";
//    url = [url stringByAppendingString:[Globle getInstance].userName];
//    NSLog(@"全部URL%@",url);
//    [[Globle getInstance].service getResultWitURL:url completeBlock:^(id result) {
//
//        NSLog(@"%@",result);
//        for (NSString *strM  in result) {
//            if([strM isEqualToString:@"固定资产投资"]){
//
//               NSString *isRefresh = result[strM];
//                NSLog(@"%@",isRefresh);
//
//
//            }
//        }
//    }];
//
//}
//

-(void)popViewDidSelectRowAtIndex:(NSInteger)index{
    
    
    self.progressLayer = [WYWebProgressLayer new];
    self.progressLayer.frame = CGRectMake(0, 42, SCREEN_WIDTH, 2);
    
    [self.navigationController.navigationBar.layer addSublayer:self.progressLayer];
    
    CHDateModel *model = self.datas[index];
    NSString *title = [NSString stringWithFormat:@"%@年%@月",model.nf,model.yf];
    [self.itemBtn setTitle:title forState:UIControlStateNormal];
    
    [self refreshDataWithModel:model];
}

-(void)refreshDataWithModel:(CHDateModel *)model{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"type"] =@"fgwoa_sjtj_gdzctz";
    params[@"nf"] = model.nf;
    params[@"yf"] = model.yf;
    params[@"username"] = [CHUUIDManager readUserName];
    NSLog(@"%@",params);
    [[Globle getInstance].service RequestWithServiceName:@"fgwoa_browseHistory" params:params httpMethod:@"POST" resultIsDictionary:false completeBlock:^(id result) {
        
        
        if (result) {
            NSString *serviceName = ServiceURL;
            NSString *urlStr = [serviceName stringByAppendingString:result];
            NSURL *url = [[NSURL alloc]initWithString:[urlStr stringByAppendingString:@"&iosversion=8"]];
            [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
            
        }
        
    }];
}

#pragma mark 检测版本号
-(void)checkVersion:(NSString *)leapAPPname
{
    if(leapAPPname != nil)
    {
        NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
        [params setValue:leapAPPname forKey:@"appname"];
        
        
        [[Globle getInstance].service RequestWithServiceName:@"lbcp_getAppVersion" params:params httpMethod:@"POST" resultIsDictionary:TRUE completeBlock:^(id result) {
            
            NSLog(@"result:%@",result);
            
            NSDictionary *dic = (NSDictionary *)result;
            if(nil != dic)
            {
                [MBProgressHUD hideHUD];
                //获取本地版本
                NSString *localVersion = VersionCode;
                NSLog(@"当前版本号%@",localVersion);
                int localVersionNUm = (localVersion == nil ? -1 : [localVersion intValue]);
                //获取服务器版本
                NSString *serverVersion = [dic valueForKey:@"appversion"];
                NSLog(@"服务器版本%@",serverVersion);
                cdnpath = [dic valueForKey:@"cdnpath"];
                int serverVersionNum = (serverVersion == nil ? -1 : [serverVersion intValue]);
                //判断是非升级
                if(localVersionNUm < serverVersionNum)
                {
                    NSString *upgrade = [dic valueForKey:@"upgrade"];
                    if([@"1" isEqualToString:upgrade])    //   强制升级
                    {
                        
                        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"有新的版本，请及时更新。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    }
                    else
                    {
                        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"有新的版本，请及时更新。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];
                    }
                    
//                    [self.myAlertView show];
                  
                    if(nil != cdnpath && ![@"" isEqualToString:cdnpath])
                    {
                        [self.myAlertView show];
                    }
                    
                }
            }
            
        }];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(alertView == self.myAlertView)
    {
        if(buttonIndex == 0)
        {
            NSString *urlStr = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",cdnpath];
            NSLog(@"全路径%@",urlStr);
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
        }
    }
    
}


@end
